<template>
	<gui-page :isLoading="pageLoading">
		<!-- 页面主体 -->
		<template v-slot:gBody>
			<view class="gui-padding">

			</view>
		</template>
	</gui-page>
</template>

<script>
	export default {
		data() {
			return {
				pageLoading: true,
			}
		},

		async onLoad(options) {
			await this.$onLaunched;
			if (options.f_parentid) {
				uni.storage.set("f_parentid", options.f_parentid)
			}

			if (options.c_parentid) {
				uni.storage.set("c_parentid", options.c_parentid)
			}
			// 新增条件判断，如果 uniacid 为 1 则直接跳转
			if (this.$store.state.base.uniacid === 1) {
				this.redirect();
				return;
			}
			// #ifdef H5

			if (options.code) {
				uni.gPost("wechat/login", {
					code: options.code,
					f_parentid: uni.storage.get("f_parentid") || 0
				}).then((res) => {
					if (res.token) {
						uni.storage.set("token", res.token);
						uni.storage.set("userinfo", res.userinfo);

						this.redirect();
					} else {
						uni.storage.set("wxuser", res);
						uni.redirectTo({
							url: "/hyb_o2o/login/login?flag=1"
						})
					}

				});
			} else {
				this.redirect()
			}
			// #endif

			// #ifndef H5
			this.redirect();
			// #endif

		},
		methods: {
			redirect() {
				let menus = this.$store.state.menus;
				let base = this.$store.state.base;

				if (menus.length > 0) {
					uni.reLaunch({
						url: "/" + uni.storage.get("pathname") + "/index/index",
						fail: () => {
							let url = menus[0]["lianjie"];

							// 修复：正确判断URL是否已有参数
							const hasParams = url.includes('?');
							const separator = hasParams ? '&' : '?';

							uni.reLaunch({
								url: url + separator + "tabbar=1",
								fail: (err) => {
									console.log(err)
								}
							})
						}
					})
				} else {
					uni.reLaunch({
						url: "/" + uni.storage.get("pathname") + "/index/index",
						fail: () => {
							let url = base.hyzx_luj;
							// 同样检查base.hyzx_luj是否有参数
							const hasParams = url.includes('?');
							const separator = hasParams ? '&' : '?';

							uni.reLaunch({
								url: url + (hasParams ? '&' : '?') + "tabbar=1",
								fail: (err) => {
									console.log(err)
								}
							})
						}
					})
				}
			}
		}
	}
</script>

<style>

</style>